//
//  BIDStoreGoodsListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDStoreGoodsListViewController.h"
#import "BIDStoreGoodsDetailViewController.h"
#import "BIDStoreGoodsCell.h"

#import <SDWebImage/UIImageView+WebCache.h>

static NSString *goodsListURL = @"commodityAndroid/list.action";

@interface BIDStoreGoodsListViewController ()<BIDBaseCollectionViewDelegate>

@end

@implementation BIDStoreGoodsListViewController
@synthesize storeID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.myCollectionView registerClass:[BIDStoreGoodsCell class] forCellWithReuseIdentifier:@"identifier"];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setItemSize:CGSizeMake(screenSize.width/2-1, (screenSize.width/2-1)*222/159)];
    [flowLayout setMinimumLineSpacing:5];
    [flowLayout setMinimumInteritemSpacing:1];
    self.myCollectionView.backgroundColor = RGBACOLOR(221, 221, 221, 1.f);
    [self.myCollectionView setCollectionViewLayout:flowLayout];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], goodsListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@&page=%ld&rows=%ld", storeID, self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myCollectionView hasNoMoreData];
            }
            else
            {
                [self.myCollectionView hasMoreData];
            }
        }
        [self.myCollectionView reloadData];
        [self autoDecideToShowEmptyView];
    }];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDStoreGoodsCell *goodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = self.dataSourceArr[row];
    goodsCell.titleLabel.text = dic[@"name"];
    goodsCell.priceLabel.text = [[NSString alloc] initWithFormat:@"￥%@", dic[@"minPrice"]];
    NSArray *fileList = dic[@"fileList"];
    if(fileList && fileList.count>0)
    {
        NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], fileList[0][@"path"]];
        [goodsCell.imgView setImageWithURL:[NSURL URLWithString:strFilePath] placeholderImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    }
    else
    {
        [goodsCell.imgView setImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    }
    cell = goodsCell;
    [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    BIDStoreGoodsDetailViewController *vc = [[BIDStoreGoodsDetailViewController alloc] initWithNibName:@"BIDStoreGoodsDetailViewController" bundle:nil];
    vc.goodsID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
